package com.icode.web.controller.house;

import com.icode.core.dto.house.EquipmentDTO;
import com.icode.core.model.house.EquipmentType;
import com.icode.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: SunKai
 * Date: 14-11-5
 * Time: 上午9:42
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/house/supporting")
public class SupportingController {
    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "form", method = RequestMethod.GET)
    public ModelAndView showForm() {
        return new ModelAndView("back/houseBase/supporting", "supporting", new EquipmentDTO());
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String submitForm(EquipmentDTO supportingDTO) {
        houseService.saveOrUpdateEquipment(supportingDTO, EquipmentType.SUPPORTING);
        return "redirect:/house/supporting/list";
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String showSupportings(Model model) throws Exception {
        List<EquipmentDTO> supportingDTOs = houseService.getEquipmentDTOs(EquipmentType.SUPPORTING);
        model.addAttribute("list", supportingDTOs);
        return "back/houseBase/supportingOverview";
    }

    @RequestMapping(value = "update/{guid}", method = RequestMethod.GET)
    public String update(@PathVariable String guid, Model model) {
        EquipmentDTO supportingDTO = houseService.getEquipmentByGuid(guid);
        model.addAttribute("supporting", supportingDTO);
        return "back/houseBase/supporting";
    }

    //    @RequestMapping(value = "delete/{guid}", method = RequestMethod.GET)
//    public String delete(@PathVariable String guid) {
//        houseService.deleteEquipmentByGuid(guid);
//        return "redirect:/house/supporting/list";
//    }
    @RequestMapping(value = "delete/{guid}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable String guid) {
        houseService.deleteEquipmentByGuid(guid);
        return true;
    }
}
